package com.atguigu.minio;

import io.minio.*;
import io.minio.errors.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @Author ChenXing
 * @Description minio 代码上传对象文件
 * @Create 2024/6/19 15:29
 */
public class MinioDemo {
    public static void main(String[] args) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {

String bucketName = "ft02";

        // 1.创建 MinioClient 对象
        MinioClient minioClient = MinioClient.builder()
                .endpoint("http://192.168.9.100:9000")
                .credentials("minioadmin", "minioadmin")
                .build();


        // 2.判断桶是否存在，不存在则创建桶，并设置访问策略

        boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!found) {
            //创建桶
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            //设置桶的访问权限
            String policy = """
                        {
                          "Statement" : [ {
                            "Action" : "s3:GetObject",
                            "Effect" : "Allow",
                            "Principal" : "*",
                            "Resource" : "arn:aws:s3:::ft02/*"
                          } ],
                          "Version" : "2012-10-17"
                        }""";
            minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(policy).build());
        }

        // 3.uploadObject 上传
        minioClient.uploadObject(
                UploadObjectArgs.builder()
                        .bucket(bucketName)
                        .object("1.jfif")
                        .filename("D:\\javacode2022\\java240328-pro_lease\\pro02-minio\\src\\main\\resources\\imgs\\1.jfif")
                        .build());


    }
}
